//
//  FBD_FMDBTool.m
//  baodongPro
//
//  Created by feng on 16/3/5.
//  Copyright © 2016年 proper. All rights reserved.
//

#import "FBD_FMDBTool.h"
#import "WeatherCityModel.h"
static FBD_FMDBTool* singleStance;
@interface FBD_FMDBTool ()
{

}
@property(nonatomic,strong) NSString* defaultDataBaseName;

@end
@implementation FBD_FMDBTool
{
    FMDatabase*_dataBase;


}

/**
 *      @author 冯宝东
 *
 *      单例模式的类构造方法
 *
 *      @return 单例 FBD_FMDBTool
 */
+(instancetype)defaultTool
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    singleStance=[[self alloc] init];
    });
    singleStance.defaultDataBaseName=@"defaultDataBaseName";
    return singleStance;
}

/**
 *      @author 冯宝东
 *
 *      给一个名字创建一个数据库
 *
 *      @param dataBaseName 数据库的名字
 *
 *      @return 是否创建成功
 */
-(FMDatabase*)db_yxCreateDataBaseWithName:(NSString*)dataBaseName
{
    if (!dataBaseName)
    {
      dataBaseName=_defaultDataBaseName;
    }
    
    NSString* db_path=[NSString stringWithFormat:@"%@/%@.sqlite",USER_Document,dataBaseName];
    
    if (!_dataBase)
    {
        _dataBase=[[FMDatabase alloc] initWithPath:db_path];
    }
    return _dataBase;
}

+(FMDatabase*)db_yxCreateDataBaseWithName:(NSString*)dataBaseName
{
    [[FBD_FMDBTool defaultTool] db_yxCreateDataBaseWithName:dataBaseName];
    return singleStance->_dataBase;
}
/**
 *      @author 冯宝东
 *
 *      给一个名字创建一张表
 *
 *      @param tableName 表明
 *
 *      @return 是否成功创建
 */
-(BOOL)db_yxCreateTableWithName:(NSString*)tableName
{

    BOOL isResut=NO;

    NSAssert(_dataBase, @"没有初始化 database  请调 db_yxCreateDataBaseWithName 函数");

     isResut= [_dataBase open];
    [_dataBase  setShouldCacheStatements:YES];
    
    if (isResut)
    {
        NSString*sql=[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, m_area_id integer NOT NULL);",tableName];
        isResut=[_dataBase executeUpdate:sql];
    }
    if (isResut) {
        NSLog(@"名字为：%@ ,table 表创建成功 ",tableName);
        
    }else
    {
        NSLog(@"名字为：%@ ,table 表创建失败 ",tableName);
    }
    isResut=[_dataBase close];

    return isResut;
}

+(BOOL)db_yxCreateTableWithName:(NSString*)tableName
{
    BOOL isResut=[[FBD_FMDBTool defaultTool]  db_yxCreateTableWithName:tableName];
    return isResut;
}

-(BOOL)db_yxInsertTableWithName:(NSString *)tableName andName:(NSString*)name withCityId:(NSInteger)cityId
{
    BOOL isResut=NO;
    
    NSAssert(_dataBase, @"没有初始化 database  请调 db_yxCreateDataBaseWithName 函数");
    
    isResut= [_dataBase open];
    [_dataBase  setShouldCacheStatements:YES];
    
    if (isResut)
    {
        NSString* sql=[NSString stringWithFormat:@"INSERT INTO %@ (name , m_area_id) VALUES(?,?);",tableName];
        isResut=[_dataBase executeUpdate:sql,name,@(cityId)];

    }
    if (isResut) {
        NSLog(@"成功插入一条数据");
        
    }else
    {
        NSLog(@"插入一条数据失败");
    }
        isResut=[_dataBase close];
    
    return isResut;

    
    
}

+(BOOL)db_yxInsertTableWithName:(NSString *)tableName andName:(NSString*)name withCityId:(NSInteger)cityId
{
    BOOL isResult=[[FBD_FMDBTool defaultTool] db_yxInsertTableWithName:tableName andName:name withCityId:cityId];

    return isResult;


}



-(BOOL)db_yxDeleteTableViewName:(NSString *)tableName withCityId:(NSInteger)cityId
{
    BOOL isResut=NO;
    
    NSAssert(_dataBase, @"没有初始化 database  请调 db_yxCreateDataBaseWithName 函数");
    
    isResut= [_dataBase open];
    [_dataBase  setShouldCacheStatements:YES];
    
    if (isResut)
    {
        NSString* sql=[NSString stringWithFormat:@"delete from %@ where m_area_id=?",tableName];
        isResut=[_dataBase executeUpdate:sql,@(cityId)];

    }
    if (isResut) {
        NSLog(@"成功删除一条数据");
        
    }else
    {
        NSLog(@"删除一条数据失败");
    }
    isResut=[_dataBase close];
    
    return isResut;



}

+(BOOL)db_yxDeleteTableViewName:(NSString *)tableName withCityId:(NSInteger)cityId
{
    BOOL isResut=NO;
    isResut= [[FBD_FMDBTool defaultTool] db_yxDeleteTableViewName:tableName withCityId:cityId];
    return isResut;
    
}

-(NSMutableArray*)db_yxSearchTableViewName:(NSString*)tableName
{
    NSMutableArray* listArray=[NSMutableArray array];
    NSAssert(_dataBase, @"没有初始化 database  请调 db_yxCreateDataBaseWithName 函数");
    BOOL  isResut= [_dataBase open];
    NSAssert(isResut, @"数据库打开失败了");
    [_dataBase  setShouldCacheStatements:YES];
    NSString* sql=[NSString stringWithFormat:@"select * from %@",tableName];
    FMResultSet* rs=[_dataBase executeQuery:sql];
    while ([rs next])
    {
        WeatherCityModel* cityModel=[[WeatherCityModel alloc] init];
        cityModel.m_userTip=[rs stringForColumn:@"name"];
        cityModel.m_area_id=[rs stringForColumn:@"m_area_id"];
        
        [listArray addObject:cityModel];
        
    }
    
    return listArray;
}
+(NSMutableArray*)db_yxSearchTableViewName:(NSString*)tableName
{
    
    NSMutableArray* listArray=[[FBD_FMDBTool defaultTool] db_yxSearchTableViewName:tableName];
    return listArray;
    
}




@end
